package com.teacold.customdemo.chapter_ten;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;

import com.teacold.customdemo.R;

public class ShapeShaderView extends View {
    private ShapeDrawable mShaperDrawable;

    public ShapeShaderView(Context context) {
        this(context,null);

    }

    public ShapeShaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
         init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mShaperDrawable=new ShapeDrawable(new RectShape());
        mShaperDrawable.setBounds(new Rect(100,100,300,300));
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.dog);
        BitmapShader bitmapShader=new BitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        mShaperDrawable.getPaint().setShader(bitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mShaperDrawable.draw(canvas);
    }
}
